package visualcbir.board;

import iautils.IAUtils;
import visualcbir.*;
import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.Observable;
import visualcbir.util.VSUtils;

/**
 *
 * @author sheila
 */
public class VSIncrementalBoard extends View{
 // int iscreated=1;
    ArrayList <int[]>aiHistograms;
  public VSIncrementalBoard(Model oModel, Controller oController){
        super(oModel, oController);
        aiHistograms=new ArrayList<int[]>();// o indice do histograma é o id da imagem que representa
        IAUtils.readHistograms(aiHistograms);

    }

    @Override
    public  IBackStructureView makeBackStructureView(){
        return null;//
    }

    /* Computes mathematical and trigonometric data needed to draw each object in the view.
     * Sets the points needed to draw images and background. Background is created
     */
    @Override
    public void update(Observable o, Object arg) { //recompute new locations
        //this.oModel.notifyObservers();

        Point oPoint=VSUtils.getCenterPoint(this.oContainer);
        //double xcenter=oPoint.x, ycenter=oPoint.y;
        double xcenter=500, ycenter=500;
        double x=xcenter,y=ycenter;
        
        //************** Creating um array de polygonos **********************//
        int numImages=100;
        int minDistance=Integer.MAX_VALUE, distance;
        int minNodeNumber=0;
        int cellSize=80;
        if(oModel.getNumImages() > 1)
        {    cellSize = 100;//(int)Math.round(Math.max( aoNodeViews.get(0).getNodeWidth(), aoNodeViews.get(0).getNodeHeight()));
        }
        for (int iNodeNumber = 0; iNodeNumber < oModel.getNumImages(); iNodeNumber++) {

            super.aoNodeViews.get(iNodeNumber).setIx((int)Math.round(x));
            super.aoNodeViews.get(iNodeNumber).setIy((int)Math.round(y));//3
//            super.aoNodeViews.get(iNodeNumber).setdScaleFactor(dScaleFactor);

            for(int i=0;i<iNodeNumber;i++){
                distance=IAUtils.compareHistograms(aiHistograms.get(i), aiHistograms.get(iNodeNumber));
                if(minDistance>distance)
                {    minDistance = distance;
                     minNodeNumber = i;
                }
            }
            aoNodeViews.get(iNodeNumber).setIx(aoNodeViews.get(minNodeNumber).getIx());
            aoNodeViews.get(iNodeNumber).setIy(aoNodeViews.get(minNodeNumber).getIy());//3

            //***************dislocando as outras até chegar ao borde
            int currentDislocated=minNodeNumber;
            
                x +=(double)cellSize;
                y +=(double)cellSize;

                int currentNode=this.findNode((int)x,(int)y);
                while( currentNode != -1){
                    aoNodeViews.get(currentDislocated).setIx((int)x);
                    aoNodeViews.get(currentDislocated).setIy((int)y);
                    x += cellSize;
                    y += cellSize;

                    currentNode=this.findNode((int)x,(int)y);
                }
                
                aoNodeViews.get(currentDislocated).setIx(aoNodeViews.get(currentNode).getIx());
                aoNodeViews.get(currentDislocated).setIy(aoNodeViews.get(currentNode).getIy());//3

            }
            //x += cellSize;
        }

      
        //super.oIBackStructureView.setObject(aoPolygons);//3
        //super.dimView.setSize(500, 500);
        //iscreated=0;
        //super.drawViewFactory(arg);//4

    
}
